<template>
  <div class="mnt-dialog form-container">
    <el-form ref="form" :model="formModel" label-width="160px" :rules="rules">
      <el-row style="text-align: center">
        <el-col :span="12">
          <el-form-item label="装配工位:" prop="ulocNo">
            <el-select
              v-model="formModel.ulocNo"
              clearable
              filterable
              :disabled="true"
            >
              <el-option
                v-for="item in ulocOptions"
                :key="item.ulocNo"
                :label="item.ulocNo+'-'+item.ulocName"
                :value="item.ulocNo"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="配置代码:" prop="configNo">
            <el-select
              v-model="formModel.configNo"
              clearable
              filterable
              :disabled="true"
            >
              <el-option
                v-for="item in configNoOptions"
                :key="item.configNo"
                :label="item.configNo+'-'+item.configDesc"
                :value="item.configNo"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="配置项代码:" prop="configItemNo">
            <el-select
              v-model="formModel.configItemNo"
              clearable
              filterable
              @change="changeConfigItemNo"
            >
              <el-option
                v-for="item in configItemNoOptions"
                :key="item.configItemNo"
                :label="item.configItemNo+'-'+item.partMaterialDesc"
                :value="item.configItemNo"
                :object="item"
              />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <el-row class="submit-area">
      <el-col :span="24">
        <el-button @click="closeDialog">取消</el-button>
        <el-button type="primary" @click="handleSubmit()">确定</el-button>
      </el-col>
    </el-row>
  </div>
</template>

<script>

export default {
  name: 'OrderConfigOptionalMnt',
  props: ['pageType', 'editData'],
  data() {
    return {
      materialDataOptions: [],
      configNoOptions: [],
      configItemNoOptions: [],
      ulocOptions: [],
      disabledNo: false,
      formModel: {
        rcrdId: '',
        orderId: '',
        vin: '',
        plantNo: '',
        materialNo: '',
        ulocNo: '',
        configNo: '',
        configItemNo: '',
        materialItemNo: '',
        materialItemDesc: '',
        updateNum: ''
      },
      configItem: {
        partMaterialNo: '',
        partMaterialDesc: ''
      },
      rules: {}
    }
  },
  computed: {},
  mounted() {
    this.init()
  },
  methods: {
    init() {
      this.$form = this.$refs.form
      const { pageType, editData, formModel } = this
      const param = { status: '1' }
      this.assignData(editData, formModel)
      this.getMaterialDataOptions(param)
      this.getUlocList(param)
      this.getConfigNoOptions(param)
      const configItemNoParam = { status: '1', configNo: this.formModel.configNo }
      this.getConfigItemNoOptions(configItemNoParam)
      this.initRules()
    },
    // 选中配置项代码
    changeConfigItemNo(value) {
      const param = { status: '1', configItemNo: value, configNo: this.formModel.configNo }
      this.get('/vehicleConfigItemNo/getVehicleConfigItemNo', param).then(res => {
        this.formModel.materialItemNo = res.partMaterialNo
        this.formModel.materialItemDesc = res.partMaterialDesc
      })
    },
    // 查询物料主数据列表
    getMaterialDataOptions(param) {
      this.get('/materialData/listMaterialData', param).then(res => {
        this.materialDataOptions = res
      })
    },
    /** 查询追溯工位列表 */
    getUlocList(param) {
      this.get('/tmBasUloc/queryTmBasUloc', param).then(res => {
        this.ulocOptions = res.list
      })
    },
    // 查询配置代码
    getConfigNoOptions(param) {
      this.get('/vehicleConfigNo/listVehicleConfigNo', param).then(res => {
        this.configNoOptions = res
      })
    },
    // 查询配置项代码
    getConfigItemNoOptions(param) {
      this.get('/vehicleConfigItemNo/listVehicleConfigItemNo', param).then(res => {
        this.configItemNoOptions = res
      })
    },
    initRules() {
      const REQUIRED_MSG = this.CONST.REQUIRED_MSG
      this.rules = {
        ulocNo: [{ required: true, message: REQUIRED_MSG, trigger: ['change', 'blur'] }],
        configNo: [{ required: true, message: REQUIRED_MSG, trigger: ['change', 'blur'] }],
        configItemNo: [{ required: true, message: REQUIRED_MSG, trigger: ['change', 'blur'] }]
      }
      this.$nextTick(_ => {
        this.$form.clearValidate()
      })
    },
    handleSubmit() {
      this.$refs.form.validate(valid => {
        if (valid) {
          const tip = '修改成功！'
          const url = '/tmOfmOrder/updateTmOfmImesBom'
          this.post(url, this.formModel).then(res => {
            this.$info(tip, () => {
              this.$emit('queryOrderConfigOptionalSuc', res)
              this.closeDialog()
            })
          })
        }
      })
    },
    closeDialog() {
      this.$parent.$emit('update:visible', false)
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
